package tool

import (
	"encoding/base64"
	"github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
	"strconv"
	"strings"
)

func GetUserFromCtx( ctx *context.Context) int {
	at:=web.AppConfig.DefaultString("beer::AuthTokenAdmin","X-AUTH-TOKEN")
	vi:=web.AppConfig.DefaultString("vi","123456")
	println(at)
	println(vi)
	header:= ctx.Request.Header.Get(at)
	println(header)
	//JsonLog(ctx.Request.Header)
	encrypt,err:=base64.StdEncoding.DecodeString(header)
	if err != nil {
		return -1
	}
	data := AesDecrypt(string(encrypt),vi)
	fields:=strings.Fields(data)
	JsonLog(fields)
	if len(fields) == 4 {
		id,_:=strconv.Atoi(fields[1])
		return id
	}
	return -1
}

func GetFrontUserFromCtx( ctx *context.Context) int {
	at:=web.AppConfig.DefaultString("beer::AuthToken","X-AUTH-TOKEN")
	vi:=web.AppConfig.DefaultString("vi","123456")
	println(at)
	println(vi)
	header:= ctx.Request.Header.Get(at)
	println(header)
	encrypt,err:=base64.StdEncoding.DecodeString(header)
	if err != nil {
		return -1
	}
	data := AesDecrypt(string(encrypt),vi)
	fields:=strings.Fields(data)
	JsonLog(fields)
	if len(fields) == 4 {
		id,_:=strconv.Atoi(fields[1])
		return id
	}
	return -1
}
